﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using Telerik.Web.UI;
using System.Collections.Generic;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTMenusSub : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private Int32 ID = -1; 
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));  

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                try
                {
                    ID = Int32.Parse(Request.QueryString["ID"]);
                    Session["ID"] = ID.ToString();
                }
                catch
                {

                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                RadTreeList1.ExpandToLevel(1);
                RadTreeList2.ExpandToLevel(1);
                if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
                    pnSubmenuOther.Visible = false;
                else
                    pnSubmenu.Visible = false;
               

            }
        }


        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbAddNew.Text = rm.GetString("lbAddNew", ci);
            lbDel.Text = rm.GetString("lbDel", ci);
            lbEdit.Text = rm.GetString("lbEdit", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbMenuSub.Text = rm.GetString("lbMenuSub", ci);
            lbBat.Text = rm.GetString("lbBat",ci);
            lbTat.Text = rm.GetString("lbTat", ci);

        }
     

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            string ID = "";
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["MenuID"].Text;
                    Menus theMenus = MenusProvider.GetMenusByID(Int32.Parse(str));
                    ID = str;
                    Count++;
                }
            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["MenuID"].Text;
                    Menus theMenus = MenusProvider.GetMenusByID(Int32.Parse(str));
                    ID = str;
                    Count++;
                }
            }

            if (Count > 1)
            {
                if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
                    ltrErorr.Text = HelpMethod.ErorrLogin("Bạn chỉ được chọn một");
                else
                    ltrErorr.Text = HelpMethod.ErorrLogin("You can only select one");
                return;
            }

            if (Count == 1)
            {
                Int32 TypeMenu = DanhMucMenusProvider.GetDanhMucMenusByID(Int32.Parse(Session["ID"].ToString())).TypeMenu;
                Menus theMenus = MenusProvider.GetMenusByID(Int32.Parse(ID.ToString()));
                String str = "";
                try
                {
                    str = theMenus.ControlUrl.Split('=')[2];
                }
                catch
                {
                    str = "";
                }
                if (TypeMenu == 2 || TypeMenu == 3)
                {
                    if(str=="")
                        Response.Redirect("EHTCreateMenuIndex.aspx?MenuID=" + ID +"&&ID=5");
                    else
                        Response.Redirect("EHTCreateMenuIndex.aspx?MenuID=" + ID + "&&ID="+TypeMenu);
                }
                else
                    Response.Redirect("EHTMenusSubUI.aspx?MenuID=" + ID + "&&TypeMenu=" + TypeMenu);
                
            }
        }

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["MenuID"].Text;
                    DataSet ds = MenusProvider.GetMenusByDanhMucID(-1, Int32.Parse(str));
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (IsVN == true)
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_Erorr);
                        else
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_ErorrOther);
                        return;
                    }
                    else
                    {
                        arr.Add(str);
                    }

                }
            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["MenuID"].Text;
                    DataSet ds = MenusProvider.GetMenusByDanhMucID(-1, Int32.Parse(str));
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_Erorr);
                        else
                            ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_ErorrOther);
                        return;
                    }
                    else
                    {
                        arr.Add(str);
                    }

                }
            }

            for (int i = 0; i < arr.Count; i++)
            {
                MenusProvider.DeleteMenus(Int32.Parse(arr[i].ToString()));
            }

                Response.Redirect("EHTMenusSub.aspx?ID=" + Session["ID"]);
        }

        protected void ImgBat_Click(object sender, ImageClickEventArgs e)
        {
            String IconTrangThai = "~//images//tick.png";
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["MenuID"].Text;
                    Count++;
                    if (item.Selected == true)
                    {
                        MenusProvider.ChangePublishMenu(Int32.Parse(str), IconTrangThai, true);
                    }
                }
            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["MenuID"].Text;
                    Count++;
                    if (item.Selected == true)
                    {
                        MenusProvider.ChangePublishMenu(Int32.Parse(str), IconTrangThai, true);
                    }
                }
            }


            if(Count>0)
                Response.Redirect("EHTMenusSub.aspx?ID=" + Session["ID"]);
        }

        protected void ImaTat_Click(object sender, ImageClickEventArgs e)
        {
            String IconTrangThai = "~//images//Close.png";
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["MenuID"].Text;
                    Count++;
                    if (item.Selected == true)
                    {
                        MenusProvider.ChangePublishMenu(Int32.Parse(str), IconTrangThai, false);
                    }
                }
            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["MenuID"].Text;
                    Count++;
                    if (item.Selected == true)
                    {
                        MenusProvider.ChangePublishMenu(Int32.Parse(str), IconTrangThai, false);
                    }
                }
            }


           if(Count>0)
               Response.Redirect("EHTMenusSub.aspx?ID=" + Session["ID"]);
        }
       

        protected void RadTreeList1_NeedDataSource(object sender, Telerik.Web.UI.TreeListNeedDataSourceEventArgs e)
        {
            RadTreeList1.DataSource = MenusProvider.GetMenusByDanhMucID(Int32.Parse(Session["ID"].ToString()), -1);           
        }

        protected void RadTreeList1_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            {
                RadTreeList1.DataSource = MenusProvider.GetMenusByDanhMucID(Int32.Parse(Session["ID"].ToString()),-1);     
                RadTreeList1.DataBind();
            }
        }

        protected void RadTreeList1_PageIndexChanged(object sender, TreeListPageChangedEventArgs e)
        {
            RadTreeList1.CurrentPageIndex = e.NewPageIndex;
            RadTreeList1.DataSource = MenusProvider.GetMenusByDanhMucID(Int32.Parse(Session["ID"].ToString()),-1);  
            RadTreeList1.DataBind();
        }

        protected void RadTreeList1_PageSizeChanged(object sender, TreeListPageSizeChangedEventArgs e)
        {
            RadTreeList1.DataSource = MenusProvider.GetMenusByDanhMucID(Int32.Parse(Session["ID"].ToString()),-1); 
            RadTreeList1.DataBind();
        }

        protected void ImgAdd_Click(object sender, ImageClickEventArgs e)
        {
            Int32 TypeMenu = DanhMucMenusProvider.GetDanhMucMenusByID(Int32.Parse(Session["ID"].ToString())).TypeMenu;
            if(TypeMenu == 1)
                Response.Redirect("EHTMenusSubUI.aspx?TypeMenu=" + TypeMenu);
            else
                Response.Redirect("EHTChoiseLinkMenu.aspx?TypeMenu="+TypeMenu);
        }


        // ngon ngu thu 2
        protected void RadTreeList2_NeedDataSource(object sender, Telerik.Web.UI.TreeListNeedDataSourceEventArgs e)
        {
            RadTreeList2.DataSource = MenusProvider.GetMenusByDanhMucID(Int32.Parse(Session["ID"].ToString()), -1);
        }

        protected void RadTreeList2_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            {
                RadTreeList2.DataSource = MenusProvider.GetMenusByDanhMucID(Int32.Parse(Session["ID"].ToString()), -1);
                RadTreeList2.DataBind();
            }
        }

        protected void RadTreeList2_PageIndexChanged(object sender, TreeListPageChangedEventArgs e)
        {
            RadTreeList2.CurrentPageIndex = e.NewPageIndex;
            RadTreeList2.DataSource = MenusProvider.GetMenusByDanhMucID(Int32.Parse(Session["ID"].ToString()), -1);
            RadTreeList2.DataBind();
        }

        protected void RadTreeList2_PageSizeChanged(object sender, TreeListPageSizeChangedEventArgs e)
        {
            RadTreeList2.DataSource = MenusProvider.GetMenusByDanhMucID(Int32.Parse(Session["ID"].ToString()), -1);
            RadTreeList2.DataBind();
        }
        

       
    }
}